//获取事件源  兼容ie下 event只读
function $src(){
	return $(event.srcElement);
}
function $key(){
	return event.which?event.which:event.keyCode;
}
/*
 * 修正与 2012-12-25
       作者:yqy
       修改内容：去掉大部分组件  与jquery结合
*/
(function(){
	var doc=document;
	var $doc=$(doc);
	window.Sys={};
	var ua=navigator.userAgent.toLowerCase();
	Sys.ie=ua.indexOf("msie")>-1?true:false;
	Sys.firefox=ua.indexOf("firefox")>-1?true:false;
	if(window.MessageEvent&&!document.getBoxObjectFor||window.openDatabase){
		Sys.chrome=ua.indexOf("chrome")>-1?true:false;
		Sys.safari=ua.indexOf("version")>-1?true:false
	}
	if(window.opera)
		Sys.opera=ua.indexOf("opera")>-1?true:false;
		
	Sys.isWin7=ua.indexOf("nt 6.1")>-1;
	Sys.isVista=ua.indexOf("nt 6.0")>-1;
	Sys.isWin2003=ua.indexOf("nt 5.2")>-1;
	Sys.isWinXp=ua.indexOf("nt 5.1")>-1;
	Sys.isWin2000=ua.indexOf("nt 5.0")>-1;
	Sys.isWindows=ua.indexOf("windows")!=-1||ua.indexOf("win32")!=-1;
	Sys.isWin=Sys.isWin7||Sys.isVista||Sys.isWin2003||Sys.isWinXp||Sys.isWin2000||Sys.isWindows;
	Sys.screen={
		w:window.screen.availWidth,
		h:window.screen.availHeight
	}
	var path_prefix="";
	var current_filename="";
	//当前页面或模块
	var current_fileprefix="";
	var pathname=location.pathname.substring(1);
	if(pathname.indexOf("/")>-1){
		pathname=pathname.substring(pathname.indexOf("/")+1);
		var path_array=pathname.split("/");
		for(var i=0;i<path_array.length-1;i++)
			path_prefix+="../";
		current_filename=path_array[path_array.length-1];
		current_fileprefix=current_filename.split(".")[0]
	}
	else{
			current_filename=pathname;
			current_fileprefix=current_filename.split(".")[0]
	}
	var webDef="/";

	if(location.pathname==webDef)
		current_fileprefix=defRoot;
	
	//通用处理对象
	var winHandle={
		cancelBubble:false,
		bubbleBegin:false,
		noRightClick:false,
		srcElem:null
	};
	var loadHandle={
		load:[],
		unload:[],
		ready:[]
	};
	//访问winHandle 媒介对象
	window.yanhx={
		//函数配置
		funcMap:{
		
		},
		//查询对象
		find:function(name){
			if(name.indexOf(".")>-1){
				var temp=name.split(".");
				var obj=this.find(temp[0]);
				if(obj!=null&&obj[temp[1]]){
					return obj[temp[1]];
				}
				else{
					return null;
				}
			}
			else{
				return winHandle[name];
			}
		},
		//添加组件
		addComponent:function(key,obj){
			winHandle[key]=obj;
		},
		//删除组件
		delComponent:function(key){
			delete winHandle[key];
		},
		//查询函数
		findFunc:function(key){
			var ret=this.funcMap[key];
			if(ret)
				return ret;
			else
				return null;
		},
		addFunc:function(key,obj){
			this.funcMap[key]=obj;
		},
		//添加带有前缀的func
		addCommon:function(key,obj){
			loadHandle[key].push(obj);
		},
		exec:function(name,param){
			if(name.indexOf(".")>-1){
				var temp=name.split(".");
				var obj=this.find(temp[0]);
				if(obj!=null&&obj[temp[1]]){
					return obj[temp[1]](param);
				}
				else{
					return this.error("对象不存在");
				}
			}
			else{
				var func=this.findFunc(name);
				if(func)
					return func(param);
				else 
					return null;
			}
		},
		error:function(mes){
			return {
				state:false,
				mes:mes
			}
		}
	};
	yanhx.addComponent("common",{
		setIndex:function(param){
			var a=param.from.css("z-index");
			var b=param.open.css("z-index");
			if(b<=a){
				param.open.css("z-index",a+5);
			}
		},
		click:function(){
			$(document.body).fire("click");
		},
		del:function(key){
			delete winHandle[key];
		},
		docOffset:function(){
			var elem=	document.documentElement;
			var obj={
				client_h:elem.clientHeight,
				client_w:elem.clientWidth,
				offset_h:elem.offsetHeight,
				offset_w:elem.offsetWidth,
				scroll_w:elem.scrollWidth,
				scroll_h:elem.scrollHeight,
				top:elem.scrollTop,
				left:elem.scrollLeft
			};
			obj.width=obj.scroll_w>obj.client_w?obj.scroll_w:obj.client_w;
			obj.height=obj.scroll_h>obj.client_h?obj.scroll_h:obj.client_h;
			return obj;
		},
		initEvent:function(e){
			if(Sys.firefox){
				window.event=e;
				window.event.srcElement=$(e.target);
				window.event.x=e.pageX;
				window.event.y=e.pageY;
			}
			//优先取which  chrome下  mouse 事件keyCode=0 
			event.keyCode=event.which?event.which:event.keyCode;
		}
	});
	yanhx.addComponent("fireBubble",function(type){
		var elem=winHandle.srcElem.parent();
		if(!elem.length)
			return;
		winHandle.srcElem=elem;
		winHandle.doBubble(type);
	});
	yanhx.addComponent("setBubble",function(val){
		winHandle.cancelBubble=val;
	});
	//冒泡事件处理
	yanhx.addComponent("doBubble",function(type){
		winHandle.setBubble(false);
		winHandle.bubbleBegin=true;
		winHandle.doEvent(type);
		winHandle.bubbleBegin=false;
		if(!winHandle.cancelBubble){
			winHandle.fireBubble(type)
		};
	});
	
	//初始化事件 便于跨平台及跨浏览器事件处理
	yanhx.addComponent("initEvent",function(e){
		if(Sys.firefox){
			window.event=e;
			window.event.srcElement=$(e.target);
			window.event.x=e.pageX;
			window.event.y=e.pageY;
		}
		//优先取which  chrome下  mouse 事件keyCode=0 
		event.keyCode=event.which?event.which:event.keyCode;
	});
	//处理事件 
	//目前支持3种事件处理  id、callfunc、bind 
	//顺序为 onclick...>callfunc>id>bind
	//初始化事件 便于跨平台及跨浏览器事件处理
	yanhx.addComponent("doEvent",function(funcType){
			//初始化事件源 冒泡则不处理
			if(!winHandle.bubbleBegin){
				winHandle.srcElem=$(event.srcElement);
			}		
			//获取声明事件
			var configFunc=winHandle.srcElem.attr("callFunc");
			if(configFunc){
				var callFun=winHandle.getEventFun(configFunc,funcType);
				if(callFun){
					callFun=callFun.split(";");
					var len=callFun.length;
					for(var i=0;i<len;i++){
						winHandle.doTypeClick(callFun[i]);	
					}
				}
			}
	});
	//获取配置信息
	yanhx.addComponent("getEventFun",function(config,func){
		if(config.indexOf(func+":")>-1){
			var str=config.substring(config.indexOf(func+":")+func.length+1,config.length);
			var temp="";
			for(var i=0;i<str.length;i++)
				if(str.charAt(i)==""||str.charAt(i)==" ")
					break;
				else 
					temp+=str.charAt(i);
			return temp
		}
		else 
			return null
	});
	//执行func
	yanhx.addComponent("doTypeClick",function(func){
		var obj=yanhx.findFunc(func);
		if(obj){
			obj.call(winHandle.srcElem);
		}else{
			yanhx.exec(func,winHandle.srcElem);
		}
	});
	//处理 ready
	yanhx.addComponent("doReady",function(func){
		yanhx.exec("myWin.init");
		$(loadHandle["ready"]).each(function(){
			this();
		});
	});
	//处理load
	yanhx.addComponent("doLoad",function(func){
		$(document.body).attr("callFunc","click:date.hideAll;mutilHandle.hideAll;tagWin.hide");
		$(loadHandle["load"]).each(function(){
			this();
		});
		//执行完后 处理所有input 添加blur事件
		$("input").each(function(){
			$(this).bind("blur",yanhx.find("fillHandle").valid);
		});
	});
	//处理unload
	yanhx.addComponent("dobeforeUnload",function(){
		$(loadHandle["unload"]).each(function(){
			this();
		});
	});
	function doDragStart(e){
		//冒泡遇到第一个 可拖动对象 结束
		winHandle.initEvent(e);
		var elem=$(event.srcElement);
		while(elem.length){
			if(elem.attr("drag")){
				yanhx.exec("dragHandle.dragStart",elem);
				break;
			}
			elem=elem.parent();
		}
	}
	//有冒泡
	function hasBubbleEvent(e,type){
		winHandle.setBubble(false);
		winHandle.initEvent(e);
		winHandle.doEvent(type);
		if(!winHandle.cancelBubble)
			winHandle.fireBubble(type);
	}
	//无冒泡
	function noBubbleEvent(e,type){
		winHandle.initEvent(e);
		winHandle.doEvent(type);
	}
	//右键事件
	doc.oncontextmenu=function(e){
		hasBubbleEvent(e,"menu");
		if(yanhx.exec("contextHandle.isRight")){
			yanhx.exec("contextHandle.setRight",false);
			return false;
		}
		/*
		top.rightClick.hiddenRight();
		yqy.setBubble(false);
		yqy.initEvent(e);
		yqy.doEvent("right");
		if(!yqy.cancelBubble)
			yqy.fireBubble("right");
		if(!yqy.srcElem.parentElement){
			rightClick.createRight();
			return false
		}
		if(yqy.noRightClick)
			return false
		*/
	};
//	检测外传blur无效果 
//	doc.onblur=function(e){
//		noBubbleEvent(e,"blur");
//	};
	doc.onfocus=function(e){
		noBubbleEvent(e,"focus");
	};
	doc.ondblclick=function(e){
		e.preventDefault();
		hasBubbleEvent(e,"clickdbl");
	};
	doc.onclick=function(e){
		hasBubbleEvent(e,"click");
		yanhx.exec("contextHandle.hide");
	};
	doc.onmousedown=function(e){
		hasBubbleEvent(e,"mousedown");
		//down 同时处理startDrag 事件 右键不处理
		if($key()==3)
			return;
		doDragStart(e);
	};
//	doc.onmouseover=function(e){
//		hasBubbleEvent(e,"mouseover");
//	};
//	doc.onmouseout=function(e){
//		hasBubbleEvent(e,"mouseout");
//	};
	doc.onselectstart=function(){
		if(yanhx.find("dragHandle").sFlag)
			return false
	};
	doc.onmouseup=function(e){
		noBubbleEvent(e,"mouseup");
		yanhx.exec("dragHandle.dragEnd");
	};
	doc.onmousemove=function(e){
		//move事件 无须监听elem  正常位document
		//noBubbleEvent(e,"mousemove");
		if(yanhx.find("dragHandle").sFlag){
			yanhx.exec("dragHandle.move");
		}
	};
	doc.onreadystatechange=function(){
		//IE某些版本 没有interactive
		if(doc.readyState=="interactive"){
			winHandle.doReady();
			winHandle.ready=true;
		}
		else if(doc.readyState=="complete"){
			if(!winHandle.ready){
				winHandle.doReady();
				winHandle.ready=true;
			}
			winHandle.doLoad()
		}
	};
	doc.onkeyup=function(e){
		noBubbleEvent(e,"keyup");
		defaultEnter(e);
	};
	doc.onmouseover=function(e){
		winHandle.initEvent(e);
		yanhx.exec("dragHandle.setDrop",$src());
	};
	window.onbeforeunload=function(){
		winHandle.dobeforeUnload();
	};
	function defaultEnter(e){
		if($key()==13){
			//回车事件
			noBubbleEvent(e,"enter");
		}
	}
	yanhx.addComponent("eventHandle",{
		add:function(param){
			if(!param.elem) return;
			//event参数 则直接替换
			if(param.event){
				param.elem.attr("callFunc",param.event);
			}
			else{
				//根据事件类型 名称 绑定  判断重复
				
			}
		}
	});
	//用于生成唯一标示
	yanhx.addComponent("idHandle",{
		prefix:'id_',
		i:parseInt(Math.random()*100),
		dataKey:'yanhx_id',
		get:function(elem){
			if(elem.attr(this.dataKey))
				return elem.attr(this.dataKey);
			else{
				var key=this.prefix+this.i++;
				elem.attr(this.dataKey,key);
				return key;
			}
		},
		isEq:function(param){
			return this.get(param.src)==this.get(param.obj);
		}
	});
	yanhx.addComponent("dragHandle",{
		sFlag:false,
		startPos:null,
		offset:null,
		curElem:null,
		src:null,
		time:null,
		dropOn:null,
		destroy:function(){
			this.startPos=null;
			this.offset=null;
			this.src=null;
			this.fireByUser=false;
			this.dropOn=null;
		},
		move:function(){
			var temp={
				x:event.x,
				y:event.y
			};
			var moveLen={
				x:temp.x-this.startPos.x,
				y:temp.y-this.startPos.y
			};
			var curCss={
				top:this.offset.top+moveLen.y,
				left:this.offset.left+moveLen.x
			};
			
			this.curElem.css(curCss);
		},
		dragEnd:function(){
			if(!this.sFlag)
				return;
			this.sFlag=false;
			if(!this.fireByUser){
				this.curElem.hide();
				this.time=window.setTimeout(this.onDrop, 50);
			}
			else{
				this.src.fire("drop");
				this.curElem=null;
				this.destroy();
			}
		},
		setDrop:function(dropOn){
			this.dropOn=dropOn;
		},
		doDrop:function(){
			if(this.dropOn){
				this.src.fire("drop");
				this.destroy();
			}
		},
		getParam:function(key){
			return this[key];
		},
		onDrop:function(){
			yanhx.exec("dragHandle.doDrop");
		},
		dragStart:function(src){
			//创建浮动层
			this.src=src;
			this.showWin();
			this.sFlag=true;
			this.startPos={
				x:event.x,
				y:event.y
			}
			this.offset=this.src.offset();
			this.curElem.css({
				top:this.offset.top,
				left:this.offset.left
			});
			this.curElem.show();
			//触发drag事件  drag 对应mousedown事件
			this.src.fire("drag");
		},
		showWin:function(){
			//不是通过属性drag触发则src作为拖动对象
			if(!this.src.attr("drag")){
				this.curElem=this.src;
				this.fireByUser=true;
				return;
			}else{
				this.curElem=$('<div class="drag_win_div"></div>');
			}
			this.curElem.html(this.src.html());
			if(!this.curElem.parent().length){
				this.curElem.appendTo(document.body);
			}
			this.curElem.css({
				width:this.src.width(),
				height:this.src.height()
			});
		}
	});
	yanhx.addComponent("fillHandle",{
		curFieldInfo:null,
		curField:null,
		defineKey:['preSave','name','fieldName',"type","bindFunc"],
		isPass:true,
		curVal:null,
		validMes:null,
		tempFields:null,
		noValid:false,
		//填充数据
		fill:function(param){
			var fields=param.elem.find(".submitField");
			var others=param.elem.find(".fillField");
			this.curVal=param.data;
			//填充 有提交到服务端数据
			this.tempFields=fields;
			this.fillFieldVal();
			//填充其他 定义为fill字段
			this.tempFields=others;
			this.fillFieldVal();
		},
		fillFieldVal:function(){
			var len=this.tempFields.length;
			for(var i=0;i<len;i++){
				this.curField=$(this.tempFields.get(i));
				this.getFieldInfo();
				if(this.curFieldInfo.bindFunc){
					yanhx.exec("bindFuncMap."+this.curFieldInfo.bindFunc,{
						elem:this.curField,
						data:this.curVal
					});
				}
				else{
					var key=this.curFieldInfo.fieldName?this.curFieldInfo.fieldName:this.curFieldInfo.name;
					var val=this.curVal[key];
					if(val==null){
						val="";
					}
					switch(this.curFieldInfo.tagName+"_"+this.curFieldInfo.type){
							//文本框
							case 'INPUT_':this.curField.val(val);break;
							case 'INPUT_password':this.curField.val(val);break;
							case 'SELECT_':this.curField.get(0).value=val.toString();break;
							case 'INPUT_text':this.curField.val(val);break;
							case 'INPUT_hidden':this.curField.val(val);break;
							//选择框
							case 'INPUT_radio':this.setCheckedVal(val);break;
							case 'INPUT_checkbox':this.setCheckedVal(val);break;
							//多行文本
							case 'TEXTAREA_':this.curField.val(val);break;
							//其他
							default:this.curField.text(val);break;
					}
				}
			}
				
		},
		submit:function(param){
			var fields=param.elem.find(".submitField");
			var len=fields.length;
			var item;
			var retBean={};
			this.isPass=true;
			this.curVal=null;
			this.validCfg=param.valid;
			for(var i=0;i<len;i++){
				this.curField=$(fields.get(i));
				//获取字段配置信息
				this.getFieldInfo();
				//提交分为 验证及不验证情况
				if(!this.validate()){
					this.isPass=false;
					this.showMes();
				}
				this.setBeanValue(retBean);
			}
			if(!this.isPass){
				//显示错误信息 
				retBean=null;
			}
			this.destroy();
			return retBean;
		},
		valid:function(e){
			if(!$(this).parentUtil("li.form_item")){
				return;
			}
			var handle=yanhx.find("fillHandle");
			var valid=null;
			if($(this).parentUtil("div.window_content")){
				var cfg=yanhx.exec("myWin.getParam");
				if(cfg&&cfg.valid&&window[cfg.valid]){
					valid=window[cfg.valid];
				}
			}else{
				valid=window["_valid_"];
			}
			if(valid){
				handle.validCfg=valid;
				handle.curField=$(this);
				handle.getFieldInfo();
				//提交分为 验证及不验证情况
				if(!handle.validate()){
					handle.showMes();
				}else{
					handle.hideMes();
				}
				handle.destroy();
			}
		},
		hideMes:function(){
			var li=this.curField.parentUtil("li.form_item");
			var valid=li.children("div.validDiv");
			if(valid.length){
				valid.hide();
				valid.find(".text").text("");
			}
		},
		showMes:function(){
			var li=this.curField.parentUtil("li.form_item");
			var valid=li.children("div.validDiv");
			if(!valid.length){
				valid=$('<div class="validDiv"><span class="flag"></span><span class="text"></span></div>').appendTo(li);
			}
			valid.find(".text").text(this.validMes);
			valid.show();
			this.validMes=null;
		},
		destroy:function(){
			this.curFieldInfo=null;
			this.curField=null;
			this.validMes=null;
			this.noValid=false;
		},
		setBeanValue:function(bean){
			var key=this.curFieldInfo.fieldName?this.curFieldInfo.fieldName:this.curFieldInfo.name;
			if(!key)return;
			if(this.curVal!=""||this.curVal=="0"||this.curVal.toString()=="false"){
				bean[key]=this.curVal;	
			}else{
				bean[key]="";	
			}
		},
		getFieldInfo:function(){
				var len=this.defineKey.length;
				this.curFieldInfo={};
				for(var i=0;i<len;i++){
					var temp=this.curField.attr(this.defineKey[i]);
					if(temp)
						this.curFieldInfo[this.defineKey[i]]=temp;	
					else
						this.curFieldInfo[this.defineKey[i]]="";
				}
				this.curFieldInfo.tagName=this.curField.get(0).tagName.toUpperCase();
		},
		getFieldValue:function(){
			//优先顺序  preSave
			var val;
			var jsonVal=this.curField.data("jsonValue");
			if(this.curFieldInfo.preSave){
				val=yanhx.exec("preSaveMap."+this.curFieldInfo.preSave,{
					info:this.curFieldInfo,
					src:this.curField
				});
			}
			else if(jsonVal){
				val=jsonVal;
			}
			else{
				switch(this.curFieldInfo.tagName+"_"+this.curFieldInfo.type){
					//文本框
					case 'INPUT_':val=this.getValue();break;
					case 'INPUT_password':val=this.getValue();break;
					case 'SELECT_':val=this.getSelectVal();break;
					case 'INPUT_hidden':val=this.getValue();break;
					case 'INPUT_text':val=this.getValue();break;
					//选择框
					case 'INPUT_radio':	val=this.getCheckedVal();break;
					case 'INPUT_checkbox':	val=this.getCheckedVal();break;
					//多行文本
					case 'TEXTAREA_':	val=this.getValue();break;
					//其他
					default:val=this.curField.text();break;
				}
			}
			this.curVal=val;	
		},
		getSelectVal:function(){
			var val=this.curField.val();
			if(val=="true")
				return true;
			if(val=="false")
				return false;
			return val;
		},
		validate:function(){
			this.getFieldValue();
			var key=this.curFieldInfo.fieldName?this.curFieldInfo.fieldName:this.curFieldInfo.name;
			if(this.validCfg&&key&&this.validCfg[key]){
				//目前添加非空校验其他 后续调整
				var temp=this.validCfg[key];
				if(temp.noNull&&!this.curVal){
					this.validMes="请输入"+temp.name;
					return false;
				}
				else if(temp.valid){
					var res=eval("this.validHandle."+temp.valid);
					if(!res){
						this.validMes=temp.errorMsg;
						return false;
					}
				}
			}
			return true;
		},
		getValue:function(){
			return this.curField.val().trim();	
		},
		getCheckedVal:function(){
			if(this.curField.attr("checked")){
				return this.curField.val();	
			}	
			else{
				return null;	
			}
		},
		setCheckedVal:function(val){
			if(this.curField.val()==val){
				this.curField.attr("checked",true);
			}
			else{
				this.curField.attr("checked",false);
			}	
		},
		validHandle:{
			number:function(start,end){
				var handle=yanhx.find("fillHandle");
				var t=handle.curVal.replace(/[0-9]/g,"");
				if(t!="")
					return false;
				if(start&&handle.curVal<start)
					return false;
				if(end&&handle.curVal>end)
					return false;
				
				return true;
			}
		}
		
	});
	//注册 分页工具
	yanhx.addComponent("pageHandle",{
		init:function(param){
			var ret=new page();
			ret.init(param);
			return ret;
		},
		getInst:function(obj){
			return obj.data("pageInst");
		}
	});
	//分页工具
	function page(){
		this.MAX=16; //界面展现页数
		this.NUM=8; //页数重构变化值
		this.pageDesc="<label>......</label>";
		this.pageDiv=null;
		this.cond=null;
		this.config=null;
		this.pageTemplates={
			normal:"<div class='pageDiv'>" +
						"<button class='preBtn'>上一页</button>" +
						"<span class='pageIndexSpan'></span>" +
						"<button class='nextBtn'>下一页</button>" +
						"<span class='pageCountSpan'>" +
							"<label>每页</label><label class='count'></label><label>条</label>"+
							"<label style='padding-left:5px;'>第</label><label class='index'></label><label>页</label>"+
						"</span>"+
						"<span class='pageGoIndexSpan'>" +
							"<input class='page_go_input' callFunc='keyup:_number'/><button class='page_go'></button>"+
						"</span>"+
					"</div>"	
		}
	}
	//查询上一页
	yanhx.addFunc("_prePage",function(){
		var pageDiv=this.parentUtil("div.pageDiv");
		var inst=yanhx.exec("pageHandle.getInst",pageDiv.parent());
		inst.cond.page-=1;
		inst.firePage();
	});
	//查询下一页
	yanhx.addFunc("_nextPage",function(){
		var pageDiv=this.parentUtil("div.pageDiv");
		var inst=yanhx.exec("pageHandle.getInst",pageDiv.parent());
		inst.cond.page+=1;
		inst.firePage();
	});
	//页面跳转
	yanhx.addFunc("_gotoPage",function(){
		var src=$src();
		var pageDiv=this.parentUtil("div.pageDiv");
		var inst=yanhx.exec("pageHandle.getInst",pageDiv.parent());
		if(src.attr("ac")){
			var p=src.text();
		}
		else{
			var p=src.parent().find("input").val();
		}
		//当前页点击不触发查询
		if(p==inst.cond.page)
			return;
		inst.cond.page=parseInt(p.trim());
		inst.firePage();
	});
	yanhx.addFunc("money",function(){
		
	});
	//只能输入数字
	yanhx.addFunc("_number",function(){
		this.val(this.val().replace(/[^0-9]/g,""));
		var key=$key();
		//回车
		if(key==13){
			var pageDiv=this.parentUtil("div.pageDiv");
			var inst=yanhx.exec("pageHandle.getInst",pageDiv.parent());
			inst.cond.page=this.val();
			inst.firePage();
		}
	});
	yanhx.addFunc("money",function(){
		r = this.val().match(/^\d+(\.\d*)?$/g);	
		this.val(this.val().match(r));
	});
	page.prototype={
		setTool:function(total){
			this.config={};
			//没有返回总记录数情况
			if(!total){
			
			}else{
				this.config.total=total;
				var temp=this.config.total%this.cond.pageSize;
				this.config.totalPage=parseInt(this.config.total/this.cond.pageSize)+(temp>0?1:0);
				this.fillPage();
			}
		},
		firePage:function(){
			if(this.cond.page<1||this.cond.page>this.config.totalPage){
				this.cond.page=parseInt(this.pageDiv.children("span.pageIndexSpan").find(".active").text());
				return;
			}
			this.func(this.cond);
		},
		init:function(param){
			this.cond=param.cond;
			this.func=param.func;
			//生成DIV
			this.pageDiv=$(this.pageTemplates.normal);
			this.initPage();
			param.table.append(this.pageDiv);
		},
		initPage:function(){
			this.pageDiv.children("button.preBtn").attr("callFunc","click:_prePage");
			this.pageDiv.children("button.nextBtn").attr("callFunc","click:_nextPage");
			this.pageDiv.find("button.page_go").attr("callFunc","click:_gotoPage");
			this.pageDiv.children("span.pageIndexSpan").attr("callFunc","click:_gotoPage");
		},
		fillPage:function(){
			var temp=this.pageDiv;
			var count=temp.children("span.pageCountSpan");
			//提供需要绑定的几个对象
			var retBean={
				indexSpan:temp.children("span.pageIndexSpan"),
				total:count.children("label.count"),
				index:count.children("label.index")
			};
			retBean.total.text(this.cond.pageSize+"/"+this.config.total);
			retBean.index.text(this.cond.page+"/"+this.config.totalPage);
			//设置上一页 下一页 等  disabled
			this.resetPageTool();
			//设置分页 
			this.resetPageSpan(this.cond.page);
		},
		resetPageTool:function(){
			var preBtn=this.pageDiv.children("button.preBtn");
			var nextBtn=this.pageDiv.children("button.nextBtn");
			var goBtn=this.pageDiv.find("button.page_go");
			if(this.config.totalPage<2){
				preBtn.attr("disabled",true);
				nextBtn.attr("disabled",true);
				goBtn.attr("disabled",true);
			}
			else{
				if(this.cond.page==1){
					preBtn.attr("disabled",true);
					nextBtn.removeAttr("disabled");
				}
				else if(this.cond.page==this.config.totalPage){
					preBtn.removeAttr("disabled");
					nextBtn.attr("disabled",true);
				}
				else{
					preBtn.attr("disabled",false);
					nextBtn.attr("disabled",false);
				}	
			}
		},
		resetPageSpan:function(curPage){
			var needDesc=false;
			if(this.config.totalPage>this.MAX){
				needDesc=true;
			}
			var temp=this.pageDiv.children("span.pageIndexSpan");
			temp.empty();
			var start=curPage-this.NUM;
			var sPage=start<0?0:start;
			var end =curPage+this.NUM;
			var ePage=end>this.config.totalPage?this.config.totalPage:end;
			var str="";
			if(start<0&&needDesc){
				str+=this.pageDesc;
			}
			for(var i=sPage;i<ePage;i++){
				if(curPage==(i+1))
					str+="<a href='javascript:void(0)' class='active' ac=true >"+(i+1)+"</a>";
				else
					str+="<a href='javascript:void(0)' ac=true >"+(i+1)+"</a>";
			}
			if(needDesc&&end>this.config.totalPage){
				str+=this.pageDesc;
			}
			temp.append($(str));
		}
	};
	//响应 callFunc事件    jquery bind 事件 则使用trigger
	yanhx.addComponent("fireEvent",function(elem,type){
		var temp=winHandle.srcElem;
		winHandle.srcElem=elem;
		winHandle.bubbleBegin=true;
		winHandle.doEvent(type);
		winHandle.bubbleBegin=false;
		winHandle.srcElem=temp;
	});
	function myCalendar(){
		this.oldKey=null;
		this.calDiv=null;	
		this.DAY=[31,28,31,30,31,30,31,31,30,31,30,31];
		this.weekDay=["星期日","星期一","星期二","星期三","星期四","星期五","星期六"];
		var temp=new Date();
		this.curY=temp.getFullYear();
		this.curM=temp.getMonth()+1;
		this.curD=temp.getDate();
		this.callerElem=null;
		this.format=["curY","curM","curD"];
		this.curDay=(function(y,m,d){
			var ret=y+"-";
			if(m<10)
				ret+="0"+m;
			else
				ret+=m;
			ret+="-";
			if(d<10)
				ret+="0"+d;
			else
				ret+=d;
			return ret;
		})(this.curY,this.curM,this.curD);
		//模板
		this.templates='<div class="calendar_div" callFunc="click:noBubble" style="display:none">'+
		'<div class="title" callfunc="mousedown:drag">'+
		'<li class="preYear" callfunc="click:date.preYear" title="上一年"></li>'+
		'<li class="preMonDiv" callfunc="click:date.preMonth" title="上一月"></li>'+
		'<li class="today_span" id="today_inner" callFunc="click:date.showYear"></li>'+
		'<li class="nextMonDiv" callfunc="click:date.nextMonth" title="下一月"></li>'+
		'<li class="nextYear" callfunc="click:date.nextYear" title="下一年"></li></div>'+
		'<div class="_my_calendar_content_">'+
		'<table class="dayofweek_div" style="width:100%;" callfunc="click:date.setDate">'+
		'<thead><tr><td>日</td><td>一</td><td>二</td><td>三</td><td>四</td><td>五</td><td>六</td></tr></thead>'+
		'<tbody></tbody></table></div><div class="canler-toolBar">'+
		'<li class="set_today" callfunc="click:date.setToday">今天</li><li class="clear" callfunc="click:date.clearDate">清空</li></div></div>';
		
		this.year=$('<div class="calendar-menu" callFunc="click:noBubble"><div class="calendar-menu-year-inner">'+
					'<span class="calendar-menu-prev" callFunc="click:date.menuPrev"></span>'+
					'<span align=center ><input class="calendar-menu-year" callfunc="keyup:_number"></span>'+
					'<span class="calendar-menu-next" callFunc="click:date.menuNext"></span>'+
					'</div><div class="calendar-menu-month-inner" callFunc="click:date.setFromMenu"><table><tbody><tr><td class="calendar-menu-month" abbr="1">一月</td><td class="calendar-menu-month" abbr="2">二月</td><td class="calendar-menu-month" abbr="3">三月</td><td class="calendar-menu-month" abbr="4">四月</td></tr><tr><td class="calendar-menu-month" abbr="5">五月</td><td class="calendar-menu-month" abbr="6">六月</td><td class="calendar-menu-month" abbr="7">七月</td><td class="calendar-menu-month" abbr="8">八月</td></tr><tr><td class="calendar-menu-month" abbr="9">九月</td><td class="calendar-menu-month" abbr="10">十月</td><td class="calendar-menu-month" abbr="11">十一月</td><td class="calendar-menu-month" abbr="12">十二月</td></tr></tbody></table></div></div>');
		this.yearInput=this.year.find("input");
	}
	myCalendar.prototype={
		hideAll:function(){
			if(!this.calDiv)
				return;
			this.year.removeClass("show");
			this.hide();
		},
		setToday:function(){
			this.callerElem.val(this.curDay);
			this.hide();
		},
		setFromMenu:function(){
			var src=$src();
			if(src.hasClass("calendar-menu-month")){
				this.curM=Number(src.attr("abbr"));
				this.curY=Number(this.yearInput.val());
				this.year.removeClass("show")
				this.reset();
			}
		},
		menuPrev:function(){
			this.yearInput.val(Number(this.yearInput.val())-1);
		},
		menuNext:function(){
			this.yearInput.val(Number(this.yearInput.val())+1);
		},
		showYear:function(){
			if(this.year.hasClass("show")){
				this.year.removeClass("show");
				return;
			}
			this.year.addClass("show");
			var c=this.calDiv.children("._my_calendar_content_");
			var o=c.offset();
			this.year.css({
				top:o.top,
				left:o.left
			});
			this.year.find("table").height(c.height());
			this.yearInput.val(this.curY);
		},
		setDate:function(){
			var src=$src();
			if(src.hasClass("day")){
				this.callerElem.val(this.createReturn(src.text()));
				this.hide();
			}
		},
		clearDate:function(){
			this.callerElem.val("");
			this.hide();
		},
		preYear:function(){
			this.curY-=1;
			this.reset();
		},
		nextYear:function(){
			this.curY+=1;
			this.reset();
		},
		nextMonth:function(){
			this.curM+=1;
			if(this.curM==13){
				this.curM=1;
				this.curY+=1;
			}
			this.reset();
		},
		preMonth:function(){
			this.curM-=1;
			if(this.curM==0){
				this.curM=12;
				this.curY-=1;
			}
			this.reset();
		},
		reset:function(){
			this.resetSecondM();
			this.setFirst();
			this.setDayContent(false);
			this.calDiv.find("#today_inner").text(this.curY+"年"+this.curM+"月");
			if(this.oldKey)
				this.body.find("#d_"+this.oldKey).addClass("today_date");
		},
		init:function(){
			this.callerElem=$src();
			if(!this.calDiv||!this.calDiv.length){
				this.calDiv=$(this.templates).appendTo(document.body);	
				this.year.appendTo(document.body);
				this.body=this.calDiv.find("tbody");
			}
		},
		setFirst:function(){
			var temp=new Date(this.curY,this.curM-1,1);
			this.firstD=temp.getDay();
		},
		setDayContent:function(todayFlag){
			this.body.empty();
			str="<tr class='day-week'>";
			var total=this.DAY[this.curM-1];
			var i=1;
			var j=1;
			for(;i<=this.firstD;i++){
				str+="<td class='null-day'></td>";
				if(i%7==0){
					str+="</tr><tr class='day-week'>";
				}
			}
			for(;i<=(total+this.firstD);i++){
				str+="<td class='day' id=d_"+this.curY.toString()+this.curM+j+">"+j+"</td>";	
				if(i%7==0){
					str+="</tr><tr class='day-week'>";
				}
				j++;
			}
			str+="</tr>";
			this.body.html(str);
		},
		open:function(){
			this.init();
			this.setOld();
			this.reset();
			this.show();
		},
		setOld:function(){
			//设置旧值
			var val=this.callerElem.val();
			var temp=val.split("-");
			var inst=this;
			this.oldKey="";
			$(this.format).each(function(i){
				if(temp[i]){
					inst[this]=parseInt(temp[i]);
					inst.oldKey+=inst[this];
				}
			});
		},
		show:function(){
			this.calDiv.css({
				display:"block",
				top:event.y+"px",
				left:event.x+"px"
			});
		},
		hide:function(){
			this.calDiv.hide();
			this.destroy();
		},
		createReturn:function(d){
			var str=this.curY+"-";//分隔符
			if(this.curM<10)
				str+="0"+this.curM;
			else
				str+=this.curM;
				
			str+="-";//分隔符
			
			if(d<10)
				str+="0"+d;
			else
				str+=d;
			return str;	
		},
		resetSecondM:function(){
			if(this.curY%4==0)
				this.DAY[1]=29;
			else
				this.DAY[0]=28;	
		},
		destroy:function(){
			this.oldKey=null;
			this.callerElem=null;
			var temp=new Date();
			this.curY=temp.getFullYear();
			this.curM=temp.getMonth()+1;
			this.curD=temp.getDate();
		}
	}
	//注册 分页工具
	yanhx.addComponent("date",new myCalendar());
	function dateFormat(){	
		this.info=null;//日期详细信息
	}
	dateFormat.prototype={
		formatInfo:function(){
			
		},
		formatDate:function(date,str){
			this.formatInfo(date);
		},
		jformatText:function(jformat, text) {
		  if (jformat==undefined) {
			return text==undefined? "": text;
		  }
		  var t1 = (jformat.toLowerCase()).indexOf("yy");
		  var t2 = (jformat.toLowerCase()).indexOf("hh");
		  if (t1>-1&&t2>-1) {							
		    return this.jformatDateTime(jformat,text);
		  } else if (t1>-1) {								
		    return this.jformatDate(jformat,text);
		  } else if (t2>-1) {								
		    return this.jformatTime(jformat,text);
		  } else if(jformat=="hiddenZero"){
			if (text==0||text=="0")
			  text = "";
			return text;
		  } else if(jformat=="%") {
			if (text==undefined||text==0||text=="0")
			  return "";
		    else
		      return text + jformat;
		  } else {
		  	return text==undefined? "": text
		  }
		},
		jformatDateTime:function (jformat,datetime){
			  if (datetime==undefined||datetime.length!=12&&datetime.length!=14) return datetime==undefined? "": datetime;
			  var sFormat = (jformat.toLowerCase()).replace(/(^\s*)|(\s*$)/g, '');
			  sFormat = sFormat.split(" ");
			  if (sFormat.length==undefined||sFormat.length<2) return datetime;
			  if (sFormat[0].indexOf("yyyy")>-1&&datetime.length==14) {
					return jformatDate(sFormat[0],datetime.substring(0,8)) + " " + jformatTime(sFormat[1],datetime.substring(8,14));
			  }	else {
					return jformatDate(sFormat[0],datetime.substring(0,6)) + " " + jformatTime(sFormat[1],datetime.substring(6,12));
			  }	 
		},
		jformatDate:function(jformat,date){
			  if (date==undefined||date.length!=6&&date.length!=8) return date==undefined? "": date;
			  var sFormat = (jformat.toLowerCase()).replace(/(^\s*)|(\s*$)/g, '');
			  var yUnit	= sFormat.substring(sFormat.lastIndexOf("y")+1,sFormat.indexOf("m"));	
			  var mUnit	= sFormat.substring(sFormat.lastIndexOf("m")+1,sFormat.indexOf("d"));	
			  var dUnit	= sFormat.substring(sFormat.lastIndexOf("d")+1,sFormat.length);		
			  if (sFormat.indexOf("yyyy")>-1&&date.length==8) {
			    return date.substring(0,4) + yUnit + date.substring(4,6) + mUnit + date.substring(6,8) + dUnit;
			  }	else {
			  	return date.substring(2,4) + yUnit + date.substring(4,6) + mUnit + date.substring(6,8) + dUnit;
			  }
		},
		jformatTime:function(jformat,date){
			  if (date==undefined||date.length!=6) return date==undefined? "": date;
			  var sFormat = (jformat.toLowerCase()).replace(/(^\s*)|(\s*$)/g, '');
			  if (sFormat.lastIndexOf("m")>sFormat.lastIndexOf("s")) {
				sFormat = sFormat.replace(/ss/ig, "mm");
				sFormat = sFormat.replace(/mm$/ig, "ss");
			  }	  
			  var hUnit	= sFormat.substring(sFormat.lastIndexOf("h")+1,sFormat.indexOf("m"));	
			  var sUnit	= sFormat.substring(sFormat.lastIndexOf("m")+1,sFormat.indexOf("s"));	
			  var mUnit	= sFormat.substring(sFormat.lastIndexOf("s")+1,sFormat.length);		
			  return date.substring(0,2) + hUnit + date.substring(2,4) +sUnit + date.substring(4,6) + mUnit;
		}	
	}
	Date.prototype.format=function(str){
		str=str.replace("yyyy",this.getFullYear());
		str=str.replace("mm",this.getM(this.getMonth()+1));
		str=str.replace("dd",this.getM(this.getDate()));
		str=str.replace("hh",this.getM(this.getHours()));
		str=str.replace("MM",this.getM(this.getMinutes()));
		str=str.replace("ss",this.getM(this.getSeconds()));
		return str;
	}
	Date.prototype.getM=function(val){
		var temp="0"+val;
		return temp.substring(temp.length-2,temp.length);
	}
	
	//注册 日期格式化
	yanhx.addComponent("dateFormat",new dateFormat());
	yanhx.addComponent("mutilHandle",{
		prefix:'l_',
		key:'key',
		dataMap:{},
		src:null,
		retDesc:null,
		elem:$("<div class='selectDiv' dateKey='name' callFunc='click:mutilHandle.setValue;noBubble'></div>"),
		preSave:function(key){
			yanhx.exec("preSaveMap.addFunc",{
				key:key,
				val:this.getBean
			});
		},
		getDesc:function(){
			var t="";
			if(this.retDesc){
				t= this.retDesc.join(";");
				this.retDesc=null;
			}
			return  t;
		},
		//cfg节点为{info:xxx,src:xxx}
		getBean:function(cfg){  
			this.retDesc=[];
			var items=cfg.src.next().find(".mutilItem");
			var ret=[];
			items.each(function(){
				var temp=yanhx.exec("mutilHandle.getItem",$(this));
				if(temp)
					ret.push(temp);
			});
			if(ret.length)
				return ret;
			else
				return null;
		},
		getItem:function(elem){
			var inputs=elem.find("input");
			var len=inputs.length;
			var ret={};
			var field=null;
			var isNull=true;
			var desc="";
			for(var i=0;i<len;i++){
				field=this.getValue($(inputs.get(i)));
				if(field&&field.key&&field.val!=""){
					ret[field.key]=field.val;
					desc+=inputs.get(i).value+" ";
					isNull=false;
				}
			}
			if(isNull)
				return null;
			else{
				this.retDesc.push(desc);
				return ret;
			}
		},
		getValue:function(elem){
			//优先取jsonValue->name  -> key属性
			var key=elem.attr("name");
			key=key?key:elem.attr(this.key);
			var val=elem.data("jsonValue");
			if(val);
			else{
				val=elem.attr("real_value");
				if(val==0||val=="0");
				else if(!val){
					val=elem.val();
				}
			}
			return {key:key,val:val}
		},
		save:function(param){
			this.dataMap[param.key]=param.val;
		},
		add:function(src){
			var pe=src.parentUtil(".mutilItem");
			var newItem=$("<div class='mutilItem'></div>").html(pe.html()).insertAfter(pe);
			newItem.find("input").removeAttr("real_value");
			pe.removeClass("firstItem");
		},
		del:function(src){
			var content=src.parentUtil(".content");
			src.parentUtil(".mutilItem").remove();
			var subs=content.children();
			if(subs.length==1){
				subs.addClass("firstItem");
			}
		},
		select:function(src){
			if(this.src){
				this.src.removeClass("hover");
			}
			this.src=src;
			var key=src.attr("key");
			if(!this.elem.parent().length){
				this.elem.appendTo(document.body);
			}
			if(this.src.hasClass("hover")){
				this.hide();
				return;
			}else{
				this.src.addClass("hover");
			}
			var val=this.dataMap[key];
			if(!val){
				this.hide();
			}else{
				this.elem.list(val,true);
				this.setPosition(src);
				this.show();
			}
		},
		show:function(){
			this.elem.show();
		},
		hideAll:function(){
			this.hide();
		},
		hide:function(){
			this.elem.hide();
			if(this.src)
				this.src.removeClass("hover");
			this.src=null;
		},
		setPosition:function(src){
			var  o=src.offset();
			this.elem.css({
				top:o.top+src.get(0).offsetHeight,
				left:o.left
			});
			this.elem.width(src.get(0).offsetWidth-2);
		},
		setValue:function(){
			var src=$src();
			//读取cb配置调用回调 没有则当做input处理
			if(this.src.attr("cb")){
				yanhx.exec(this.src.attr("cb"),{
					val:src.data("data"),
					src:this.src
				});
				this.hide();
				return;
			}
			var old={
				id:this.src.attr("real_value"),
				name:this.src.text()
			};
			var val=src.data("data");
			this.src.val(val.name);
			this.src.attr("real_value",val.id);
			if(val.id!=old.id){
				this.src.fire("change");
			};
			this.hide();
		}
	});
	$(window).bind("resize",function(){
		$(document.body).fire("click");
	});
	yanhx.addComponent("ajaxHandle",{
		uqKey:'domainAjax',
		uqId:parseInt(Math.random()*1000),
		objMap:{},
		parseParam:function(param){
			var ret=[];
			for(var k in param){
				ret.push(k+"="+param[k]);
			}
			if(ret.length)
				return "&"+ret.join("&");
			else
				return "";
		},
		get:function(param){
			var key=this.uqKey+this.uqId++;	
			this.add(key,param.callback);
			param.url+=key;
			if(param.param){
				param.url+=this.parseParam(param.param);
			}
			var elem=$('<script src='+param.url+' type="text/javascript"></script>')
			.appendTo(document.head);
			this.objMap[key+"_elem"]=elem;
			yanhx.exec("myWin.showload");
		},
		add:function(key,func){
			this[key]=func;
		},
		callback:function(param){
			yanhx.exec("myWin.hideload");
			this[param.call](param.data);
			delete this[param.call];
			this.objMap[param.call+"_elem"].remove();
			delete this.objMap[param.call];
			param=null;
		}
		
	});
	yanhx.addComponent("tagWin",{
		template:$('<div class="balloon" callFunc="click:noBubble"><div class="balloon_pointer"><div class="balloon_arrow_border"></div>'+
				 '</div><div class="balloon_content"></div></div>'),
		ctrl:'<div style="text-align:right;padding-right:3px;">'+
		 '<button class="btn_3" callFunc="click:tagWin.doBack;tagWin.hide">确定</button></div>',
		open:function(param){
			if(!this.template.parent().length){
				this.template.appendTo(document.body);
				this.content=this.template.find(".balloon_content");
			}
			this.doBack=param.callback;
			this.content.empty();
			this.content.append(param.elem);
			this.content.append(this.ctrl);
			var o=$src().offset();
			this.template.css({
				top:o.top+$src().height()+10,
				left:o.left
			});
			this.template.show();
			param=null;
		},
		hide:function(){
			this.template.hide();
		},
		doBack:function(){
			
		}
	});
	$.fn.tagWin=function(callback){
		yanhx.exec("tagWin.open",{
			elem:$(this),
			callback:callback
		});
	};
	yanhx.addComponent("scrollHandle",{
		scrollMap:[],
		add:function(func){
			this.scrollMap.push(func);
		},
		scroll:function(){
			if(this.scrollMap.length){
				var len=this.scrollMap.length;
				for(var i=0;i<len;i++){
					this.scrollMap[i]();
				}
			}
		}
	});
	$(window).bind("scroll",function(e){
		var total=document.documentElement.scrollHeight;
		var client=$(window).height();
		var scrollTop=$(window).scrollTop();
		if(total<=(client+scrollTop)){
			yanhx.exec("scrollHandle.scroll");
		}
	});
})(jQuery);	

